<?php
namespace Modules\Taxonomy\Library;

use MongoDB\BSON\ObjectID;
use Phalcon\Events\Event;
use Phalcon\Forms\ElementInterface;
use Phalcon\Di;

class ElementListener
{
    public function getDefault(Event $event, ElementInterface $element, $data)
    {
        $options = $element->getUserOptions();
        if (isset($options['term']) && is_array($options['term'])) {
            if (!$data) {
                $data = [];
            }
            if (is_string($data)) {
                $data = explode(',', $data);
            }
            $entityManager = Di::getDefault()->getShared('entityManager');
            $output = [];
            switch ($options['widget']) {
                case 'Tags':
                    foreach ($data as $oid) {
                        $term = $entityManager->get('term', [
                            'conditions' => [
                                '_id' => new ObjectID($oid)
                            ]
                        ]);
                        if ($term) {
                            $output[] = $term->name;
                        }
                    }
                    break;
                case 'Select':
                    break;
            }
            $element->setDefault(implode(',', $output));
        }
    }

    public function getValue(Event $event, ElementInterface $element, $data)
    {
        $options = $element->getUserOptions();
        if (isset($options['term']) && is_array($options['term'])) {
            if (!$data) {
                $data = [];
            }
            if (is_string($data)) {
                $data = explode(',', $data);
            }
            $entityManager = Di::getDefault()->getShared('entityManager');
            $output = [];
            switch ($options['widget']) {
                case 'Tags':
                    foreach ($data as $oid) {
                        if (empty(trim($oid))) {
                            continue;
                        }
                        $termExist = true;
                        $query = [
                            'conditions' => [
                                'name' => $oid
                            ]
                        ];
                        if (isset($options['term']['taxonomy'])) {
                            $query['conditions']['contentModel'] = $options['term']['taxonomy'];
                        }
                        $term = $entityManager->get('term', $query);
                        if (!$term && isset($options['term']['add']) && $options['term']['add'] && isset($options['term']['taxonomy'])) {
                            $term = $entityManager->get('term');
                            $term->name = $oid;
                            $term->parent = isset($options['term']['parent']) ? $options['term']['parent'] : '0';
                            $term->description = '';
                            $term->contentModel = $options['term']['taxonomy'];
                            $term->other = [];
                            $termExist = $term->save();
                        }
                        if ($termExist) {
                            $output[] = (string)$term->getId();
                        }
                    }
                    break;
            }
            $element->setValue($output);
        }
    }
}